<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <script>
        var arr = [2, 3, 4, 15, 23, 16, 36, 20];
        // for (var i=0;i<arr.length;i++){
        //     console.log(arr[i]);
        // }

        // //foreach 数组遍历
        // arr.forEach(function(ele,index,arrya){
        //     console.log(ele,index,arryay);
        // })

        var list = document.querySelectorAll('li');
        //foreach 数组遍历
        list.forEach(function (ele, i) {
            ele.addEventListener('click', function () {
                console.log(ele);
            })
        })

        //map,对元素操作(加减乘除),返回新数组,数组长度不变
        var newArr1 = arr.map(function (ele, i, array) {
            return ele > 20 ? ele + 5 : ele;
        })

        //filter,过滤,返回符合条件的元素组成的新数组
        var newArr2 = arr.filter(function (ele, i) {
            if (ele % 5 == 0) {
                return ele;
            }
        })

        //some,是否有满足条件的元素  返回true/flase
        var bool1 = arr.some(function (ele, i) {
            return ele > 50;
        })

        //every,所有元素是否满足条件  返回true/false
        var bool2 = arr.every(function (ele, i) {
            return ele > 50;
        })

        //find,返回第一个满足条件的元素,否则返回undefined
        var found = arr.every(function (ele, i) {
            return ele > 50;
        })

        //reduce,累加迭代器,从给定的第二个参数开始加数组中的元素,最后第二个参数不变
        var total = 0; //求和的初始值,如果不指定,默认为0
        var sum = arr.reduce(function (r, ele) {
            return r + ele;
        }, total);//function为第一个参数,total为第二个参数

    </script>
</body>

</html>